<!DOCTYPE html>
<!-- Tutorial series by David Rousset: http://blogs.msdn.com/davrous -->
<!-- Based on this EaselJS sample: http://easeljs.com/examples/bitmapAnimation.html -->
<html>

<head>
    <title>dashPlatformerTest</title>

    <!-- Import EaselJS Framework -->
    <script src="js/jquery-2.1.1.min.js"></script>
    <script src="js/easeljs-0.6.0.min.js"></script>
    <script src="js/gamefwk/util/ImgLoader.js"></script>
    <script src="js/gamefwk/Vector2.js"></script>
    <script src="js/gamefwk/Framework.js"></script>
    <script src="js/gamefwk/Rectangle.js"></script>
    <script src="js/gamefwk/Color.js"></script>

    <script src="js/gamefwk/go/GameObject.js"></script>
    <script src="js/gamefwk/go/char/Sonic1.js"></script>
    <script src="js/gamefwk/go/char/Platform.js"></script>

    <script src="js/gamefwk/go/cont/Controller.js"></script>
    <script src="js/gamefwk/go/cont/SimplePlatformerController.js"></script>
    <script src="js/gamefwk/go/cont/Sonic2DController.js"></script>
    <script src="js/gamefwk/go/cont/PlatformerJumpController.js"></script>

    <script src="js/Box2dWeb-2.1.a.3.min.js"></script>
    <script src="js/gamefwk/go/upd/Updater.js"></script>
    <script src="js/gamefwk/go/upd/SimplePlatformerCollisionUpdater.js"></script>
    <script src="js/gamefwk/go/upd/Sonic2DCollisionUpdater.js"></script>
    <script src="js/gamefwk/go/upd/DeathByFallUpdater.js"></script>

    <script src="js/gamefwk/go/gen/Gen.js"></script>
    <script src="js/gamefwk/go/gen/BrickGen.js"></script>
    <script src="js/gamefwk/map/MapGen.js"></script>

    <script src="js/goog/base.js"></script>
    <script src="js/goog/math/rect.js"></script>

</head>

<body id="body">

    <canvas id="testCanvas" width="800" height="600" style="background-color:gray">
        Your browser doesn't support canvas. Please download IE9+ on <a href="http://ie.microsoft.com/testdrive">IE Test Drive</a>
    </canvas>


    <script>
    GameSys = {};

    GameSys.canvas = document.getElementById("testCanvas");
    GameSys.ctx = GameSys.canvas.getContext("2d");

    GameSys.fps = 60;
    GameSys.imagesToLoad = [{
        path: "img/platform.png",
        id: "platform"
    }, {
        path: "img/sonic1_1.png",
        id: "sonic1"
    }];
    GameSys.imgLoader = new ImgLoader(GameSys.imagesToLoad.length, startGame);
    GameSys.gameObjects = [];

    GameSys.drawScale = 10;
    GameSys.debugModeOn = false;
    GameSys.putMario = false;

    GameSys.screenScale = 0.9;
    GameSys.width = getWidth() * GameSys.screenScale;
    GameSys.height = getHeight() * GameSys.screenScale;

    GameSys.defHeroPos = {
        x: GameSys.width / 2,
        y: GameSys.height / 2
    }

    var GRID_HORIZONTAL = 8;
    var GRID_VERTICAL = 4;

    $(document).ready(init);

	/*este es un comentario*/
    function loadImages() {
            for (var i = 0; i < GameSys.imagesToLoad.length; i++) {
                GameSys.imgLoader.load(GameSys.imagesToLoad[i].path, GameSys.imagesToLoad[i].id);
            }
        } //loadImages

    function initWorld() {
            // create a new stage and point it at our canvas:
            GameSys.stage = new createjs.Stage(GameSys.canvas);
            GameSys.gameContainer = new createjs.Container();
        } //initWorld

    function initGameObjects() {
            GameSys.platforms = GameSys.platforms || [];
            var platforms = GameSys.platforms;
            var platformScale = 1.5;


            var firstPlatformPos = {
                x: GameSys.width / 2,
                y: GameSys.height / 2 + 75
            }


            addPlatforms();



            var heroScale = 1.5;
            /*var hero = new Sonic2(GameSys.width / 2, GameSys.height / 2);*/
            var hero = new Sonic1(GameSys.defHeroPos.x, GameSys.defHeroPos.y, heroScale, heroScale);
            GameSys.hero = hero;
            GameSys.gameObjects.push(hero);
            var cont = new Sonic2DController(GameSys.hero, 5, 10);
            var onDebug = function() {
                if (input.space) {
                    console.log(getBounds(this));
                }
                if (input.i) {
                    var imgr = this.animation.spriteSheet._frames[this.animation.currentFrame];
                    console.log(imgr);
                }
                if (input.h) {
                    console.log(this);
                }
                if (input.r) {
                    reset();
                }
            }
            var onDebugg = onDebug.bind(hero);
            cont.onDebug = onDebugg;
            hero.addController(cont);
            var updater = new Sonic2DCollisionUpdater(hero, platforms, 0.3, 7);
            hero.addUpdater(updater);
            var dupdater = new DeathByFallUpdater(hero, 900, reset);
            hero.addUpdater(dupdater);


            turnOnDebugDraw();
        } //initGameObjects

    function turnOnDebugDraw() {
        var go = GameSys.gameObjects;
        for (var i = 0; i < go.length; i++) {
            go[i].debugDrawOn = true;
        }
    }

    function init() {
            setCanvasSize(GameSys.width, GameSys.height);

            initWorld();

            loadImages();

            initGameObjects();
        } //init

    function loadAnimations() {
            for (var i = 0; i < GameSys.gameObjects.length; i++)
                GameSys.gameObjects[i].loadAnimations();
        } //loadAnimations


    function loadWorld() {
        GameSys.background = self.createBgGrid(GRID_HORIZONTAL, GRID_VERTICAL, GameSys.width, GameSys.height);
        GameSys.stage.addChild(GameSys.background);


        for (var i = 0; i < GameSys.gameObjects.length; i++) {
            GameSys.gameContainer.addChild(GameSys.gameObjects[i].animation);
        }

        GameSys.stage.addChild(GameSys.gameContainer);
    }

    function startGame() {
            loadAnimations();


            GameSys.background = self.createBgGrid(GRID_HORIZONTAL, GRID_VERTICAL, GameSys.width, GameSys.height);
            GameSys.stage.addChild(GameSys.background);


            for (var i = 0; i < GameSys.gameObjects.length; i++) {
                GameSys.gameContainer.addChild(GameSys.gameObjects[i].animation);
            }

            GameSys.stage.addChild(GameSys.gameContainer);


            // we want to do some work before we update the canvas,
            // otherwise we could use Ticker.addListener(stage);
            createjs.Ticker.addListener(GameSys);
            createjs.Ticker.useRAF = true;
            // Best Framerate targeted (60 FPS)
            createjs.Ticker.setFPS(GameSys.fps);
        } //startGame


    function reset() {
        var stage = GameSys.stage;
        var world = GameSys.gameContainer;
        var hero = GameSys.hero;
        var go = GameSys.gameObjects;

        stage.removeAllChildren();
        world.removeAllChildren();
        world.x = world.y = 0;

        hero.x = GameSys.defHeroPos.x;
        hero.y = GameSys.defHeroPos.y;

        /*for (var i = 0; i < go.length; i++) {
            world.addChild(go[i].animation);
        }*/

        loadWorld();
        //comment
    }


    function addPlatform(x, y, scaleX, scaleY) {
            GameSys.platforms = GameSys.platforms || [];

            x = Math.round(x);
            y = Math.round(y);

            var platform = new Platform(x, y, scaleX, scaleY);

            /*if ( Math.random() < .35 ) {
                self.addWaterfall(platform);
            }*/

            var world = GameSys.gameContainer;
            var platforms = GameSys.platforms;

            platforms.push(platform);
            GameSys.gameObjects.push(platform);
            GameSys.lastPlatform = platform;

            return platform;
        } //addPlatform


    function movePlatformToEnd(platform) {
            var absW = platform.getAbsDimensions().w;

            platform.x = GameSys.lastPlatform.x + absW * 2 + (Math.random() * absW * 2 - absW);
            platform.y = GameSys.lastPlatform.y + (Math.random() * 300 - 150) * scale;
            GameSys.lastPlatform = platform;
        } //movePlatformToEnd

    function addPlatforms() {
            var w = GameSys.width;
            var h = GameSys.height;
            var platDefWidth = Platform.DEF_WIDTH;
            var BASE_WIDTH = 800;
            var BASE_HEIGHT = 400;
            var scale = snapValue(Math.min(w / BASE_WIDTH, h / BASE_HEIGHT), .5)

            console.log("scale="+scale);

            var c;
            var l = w / (platDefWidth * 1.5) + 2;
            var atX = 0;
            var atY = h / 1.25;

            for (c = 1; c < l; c++) {
                var atX = (c - .5) * platDefWidth * 2 + (Math.random() * platDefWidth - platDefWidth / 2);
                var atY = atY + (Math.random() * 300 - 150) * scale;
                addPlatform(atX, atY);
            }
        } //addPlatforms


    GameSys.tick = function() {
        GameSys.stage.update();

        var hero = GameSys.hero;
        var world = GameSys.gameContainer;
        var w = GameSys.width;
        var h = GameSys.height;

        var wd = 0.5;
        var hd = 0.6;


        writeOnCanvas("getBounds=" + JSON.stringify(getBounds(hero)), 25, 25, "red");
        writeOnCanvas("getRectBounds=" + JSON.stringify(hero.getRectBounds()), 25, 50, "red");

        adjustScrolling(hero, world, w, h, wd, hd);
        for (var i = 0; i < GameSys.gameObjects.length; i++) {
            GameSys.gameObjects[i].tick();
        }


        GameSys.background.x = (world.x * .45) % (w / GRID_HORIZONTAL);
        GameSys.background.y = (world.y * .45) % (h / GRID_VERTICAL);

    };
    //tick
    </script>

</body>

</html>
